package sifcom.Entidades.Factory;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

import sifcom.Entidades.Canton2;
import sifcom.Entidades.Provincia;
import sifcom.Utilidades.Utilidades;
import sifcom.json.ManejoJson;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;

public class CantonFactory {
	
	public CantonFactory()
	{
		
	}
	
	public CantonFactory(BufferedReader reader) throws JSONException, EntityNotFoundException
	{
		ObjectifyGenericDao<Canton2> daoObjectify = new ObjectifyGenericDao<Canton2>(Canton2.class);
		//ObjectifyGenericDao<Provincia> daoProv = new ObjectifyGenericDao<Provincia>(Provincia.class);
		
		Utilidades util = new Utilidades();
		
		JSONObject jsonSocio = util.getJsonObject(reader);
		
			//Delete
			if(jsonSocio.length() == 1 && jsonSocio.get("id") != null)
			{
				//com.google.appengine.api.datastore.Key keyC = KeyFactory.stringToKey(jsonSocio.get("id").toString());
				
				Key<Canton2> keyCanton = new Key<Canton2>(Canton2.class,jsonSocio.getLong("id")); 
				
				Objectify ofy = daoObjectify.fact().begin();			
				
				ofy.delete(keyCanton);
			}
			
			//Create
			if(jsonSocio.length() > 1 && jsonSocio.get("id").toString().length() == 4)
			{
				Canton2 canton2 = new Canton2();
				
				Key<Provincia> keyProv = new Key<Provincia>(Provincia.class, new Long(jsonSocio.get("provincia").toString()));
				canton2.setProvincia(keyProv);
				
				ManejoJson<Canton2> mson = new ManejoJson<Canton2>(Canton2.class);
				mson.setData(canton2, jsonSocio);
				
				Key<Canton2> key =daoObjectify.put(canton2);
				
				//Key<Canton2> cnt = new Key<Canton2>(Canton2.class,54);
				//Canton2 c = daoObjectify.get(cnt);
				System.out.println(key.getId());
				System.out.println(key.getRaw());
			}
		
			//Update
			if(jsonSocio.length() > 1 && !jsonSocio.get("id").toString().equalsIgnoreCase("null"))
			{
				//Entity ent = util.getEntity(jsonSocio.get("id").toString());
				Key<Canton2> key = new Key<Canton2>(Canton2.class,jsonSocio.getLong("id"));
				
				System.out.println("Key: "+key.getId());
				
				Canton2 Canton2 = daoObjectify.get(key);
				
				if(!jsonSocio.isNull("provincia"))
				{
					Key<Provincia> keyProv = new Key<Provincia>(Provincia.class, new Long(jsonSocio.get("provincia").toString()));
					Canton2.setProvincia(keyProv);
				}
				
				ManejoJson<Canton2> mson = new ManejoJson<Canton2>(Canton2.class);
				mson.setData(Canton2, jsonSocio);
				daoObjectify.put(Canton2);				
			}
	}
	
	public String getData() throws EntityNotFoundException{
		
		ObjectifyGenericDao<Canton2> daoObjectify = new ObjectifyGenericDao<Canton2>(Canton2.class);		
		
		List<Canton2> lst = daoObjectify.listByExample(new Canton2());
		//ManejoJson<Canton2> mjson = new ManejoJson<Canton2>(Canton2.class);
		return setData(lst);
	}

	public String Search(String Canton2) throws EntityNotFoundException{
		
		ObjectifyGenericDao<Canton2> daoObjectify = new ObjectifyGenericDao<Canton2>(Canton2.class);
		Canton2 prov = new Canton2();
		if(Canton2.length()>=1)
		prov.setCanton(Canton2);
		
		List<Canton2> lst = daoObjectify.listByExample(prov);
		//ManejoJson<Canton2> mjson = new ManejoJson<Canton2>(Canton2.class);

		return setData(lst);
	}	
	
	public String SearchByProvincia(String id) throws EntityNotFoundException{
		
		ObjectifyGenericDao<Canton2> daoObjectify = new ObjectifyGenericDao<Canton2>(Canton2.class);
		ObjectifyGenericDao<Provincia> daoProvincia = new ObjectifyGenericDao<Provincia>(Provincia.class);
		
		Key<Provincia> keyProv = new Key<Provincia>(Provincia.class,new Long(id));
		Canton2 prov = new Canton2();
		prov.setProvincia(keyProv);
		
		//List<Canton2> lst = daoObjectify.listByExample(prov);
		//ManejoJson<Canton2> mjson = new ManejoJson<Canton2>(Canton2.class);
		Objectify obj = daoObjectify.fact().begin();
		//Provincia p = daoProvincia.get(keyProv);
		List<Canton2> lst = obj.query(Canton2.class).filter("provincia", keyProv).list();
		return setData(lst);
	}	
	
	private String setData(List<Canton2> lst) throws EntityNotFoundException
	{
		JSONObject json = new JSONObject();
		List<String> array= new ArrayList<String>();
		
		ObjectifyGenericDao<Provincia> daoObjectify = new ObjectifyGenericDao<Provincia>(Provincia.class);
		Objectify ofy = daoObjectify.fact().begin();
		
		for(Canton2 entidad : lst)
		{			
				try {
					//if(entidad.getId() >= 50)					
					{
					//com.google.appengine.api.datastore.Key keyCanton2 = daoObjectify.fact().getRawKey(entidad);						
					//KeyFactory.keyToString(keyCanton2)
						
					json.put("id", entidad.getId());
					json.put("canton", entidad.getCanton());
					//Entity prov = daoObjectify.fact().begin().getDatastore().get(entidad.getProvincia().getRaw());
					json.put("provincia", ofy.get(entidad.getProvincia()).getId());
					
					array.add(json.toString());
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
		}	
		
					
		
		String myOut = "{\"success\":true,\"message\":\"Load Data 2\",\"data\":"+array.toString()+"}"; 
		
		return myOut;
	}
	
}
